package com.ming.yygh.hosp.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ming.yygh.common.exception.HospitalException;
import com.ming.yygh.common.helper.HttpRequestHelper;
import com.ming.yygh.common.result.Result;
import com.ming.yygh.common.result.ResultCodeEnum;
import com.ming.yygh.common.utils.MD5;
import com.ming.yygh.hosp.service.DepartmentService;
import com.ming.yygh.hosp.service.HospitalService;
import com.ming.yygh.hosp.service.HospitalSetService;
import com.ming.yygh.hosp.service.ScheduleService;
import com.ming.yygh.model.hosp.Department;
import com.ming.yygh.model.hosp.Hospital;
import com.ming.yygh.model.hosp.HospitalSet;
import com.ming.yygh.model.hosp.Schedule;
import com.ming.yygh.vo.hosp.DepartmentQueryVo;
import com.ming.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Light
 * @date 2021/12/1 17:54
 */
@RestController
@RequestMapping("api/hosp")
public class ApiController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    // 上传医院接口
    @PostMapping("/saveHospital")
    public Result<Boolean> saveHospital(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 1.获取医院系统中传递过来的签名,签名进行了MD5加密
        String hospSign = (String) paramMap.get("sign");
        // 2.根据传递过来的医院编号，查询数据库，查询签名比对
        String hoscode = (String) paramMap.get("hoscode");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        // 3.把数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        // Base64传输过程中“+”转换为了“ “，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        // 调用service方法
        boolean save = hospitalService.save(paramMap);
        return Result.ok(save);
    }

    // 查询医院接口
    @PostMapping("/hospital/show")
    public Result<Hospital> getHospital(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 1.获取医院系统中传递过来的签名,签名进行了MD5加密
        String hospSign = (String) paramMap.get("sign");
        // 2.根据传递过来的医院编号，查询数据库，查询签名比对
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        // 3.把数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        // 4.判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    // 上传科室接口
    @PostMapping("/saveDepartment")
    public Result<Boolean> saveDepartment(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 签名验证
        String hoscode = (String) paramMap.get("hoscode");
        // MD5加密后的的签名
        String hospSign = (String) paramMap.get("sign");
        // 根据医院编号，查询数据库，查询签名比对
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        // 把数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    // 查询科室接口
    @PostMapping("/department/list")
    public Result<Page<Department>> findDepartment(HttpServletRequest request) {
        // 获取传递来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 当前页和每页记录数
        int page = StringUtils.hasLength(String.valueOf(paramMap.get("page"))) ? Integer.parseInt((String) paramMap.get("page")) : 10;
        int limit = StringUtils.hasLength(String.valueOf(paramMap.get("limit"))) ? Integer.parseInt((String) paramMap.get("limit")) : 10;
        // 签名校验
        String hospSign = (String) paramMap.get("sign");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        // 调用service方法
        Page<Department> departmentPage = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    // 删除科室接口
    @PostMapping("/department/remove")
    public Result<Boolean> removeDepartment(HttpServletRequest request) {
        // 获取传递来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        // 医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 签名校验
        String hospSign = (String) paramMap.get("sign");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    // 上传排班接口
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 查询排班接口
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取传递来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 当前页和每页记录数
        int page = StringUtils.hasLength(String.valueOf(paramMap.get("page"))) ? Integer.parseInt((String) paramMap.get("page")) : 10;
        int limit = StringUtils.hasLength(String.valueOf(paramMap.get("limit"))) ? Integer.parseInt((String) paramMap.get("limit")) : 10;
        // 签名校验
        String hospSign = (String) paramMap.get("sign");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageSchedule = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageSchedule);
    }

    // 删除排班接口
    @PostMapping("/schedule/remove")
    public Result<Boolean> removeSchedule(HttpServletRequest request) {
        // 获取传递来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        // 签名校验
        String hospSign = (String) paramMap.get("sign");
        HospitalSet hospitalSet = hospitalSetService.getOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        String signKey = hospitalSet.getSignKey();
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
